package com.shu.helloworld;


import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;


public class HelloService extends Service {
    private BroadcastReceiver mBroadcastReceiver;
    private static int percent = 5;
    private int level = 0;
    private int scale = 100;

    public HelloService() {
        try {
            mBroadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    if (action.equals("android.intent.action.BATTERY_CHANGED")) {
                        level = intent.getExtras().getInt("level", 0);//当前电量
                        scale = intent.getExtras().getInt("scale", 100);//总电量
                        percent = level * 10 / scale;
                    }
                    UpdataImage(context);
                }


            };
        } catch (Exception e) {
            e.printStackTrace();

        }


    }

    private int[] drawables = {
            R.drawable.helloworld0,
            R.drawable.helloworld1,
            R.drawable.helloworld2,
            R.drawable.helloworld3,
            R.drawable.helloworld4,
            R.drawable.helloworld5,
            R.drawable.helloworld6,
            R.drawable.helloworld7,
            R.drawable.helloworld8,
            R.drawable.helloworld9,
            R.drawable.helloworld10,
    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        try {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(mBroadcastReceiver, intentFilter);

        } catch (Exception e) {
            e.printStackTrace();

        }

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                String id = "channel_1";
                String description = "520";
                int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel channel = new NotificationChannel(id, description, importance);//创建一个通知渠道

                notificationManager.createNotificationChannel(channel);

                Notification build = new Notification.Builder(this, id)
                        .setCategory(Notification.CATEGORY_EVENT)
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle("一行瑠璃")
                        .setContentText("容器与内容匹配")
                        .setAutoCancel(true)
                        .setChannelId(id)//指定channelID，就是
                        .build();

                startForeground(1, build);



        }

    }

    private void UpdataImage(Context context) {
        AppWidgetManager instance = AppWidgetManager.getInstance(context);
        ComponentName componentName = new ComponentName(context, HelloWorld.class);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.hello_world);
        remoteViews.setImageViewResource(R.id.container_image, drawables[percent]);
        instance.updateAppWidget(componentName, remoteViews);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        UpdataImage(getApplicationContext());
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public void onDestroy() {
        unregisterReceiver(mBroadcastReceiver);
        //mNotificationManager.cancel(1);
        super.onDestroy();
    }
}
